package com.zq.leave.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zq.leave.entity.Vacate;
import com.zq.leave.entity.dto.Result;
import com.zq.leave.service.VacateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;


/**
 * @Author: baicun
 * @CreateTime: 2020-06-22
 * @Description: 提供图表统计的数据类
 */
@RestController
@CrossOrigin
@RequestMapping("/count")
public class  CountController {

    @Autowired
    private VacateService vacateService;

    //统计User请假成功，失败，审批中，待确认，审批不通过的数据
    @PostMapping("/countVacate")
    public Result countVacate(@RequestBody JSONObject jsonObject){
        String username = jsonObject.getStr("username");
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int vacatedYesCount,vacatedNoCount, approvingCount ,approvedYesCount,approvedNoCount;
            //user
        vacatedYesCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("username",username).eq("status","请假成功"));
        vacatedNoCount = vacateService.count(new QueryWrapper<Vacate>().
                eq("username",username).eq("status","请假失败"));
            //正在进行
        approvingCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("username",username).eq("status","待审批"));
            //待确认数
        approvedYesCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("username",username).eq("status","待确认"));
        approvedNoCount = vacateService.count(new QueryWrapper<Vacate>().
                eq("username",username).eq("status","不通过"));

        map.put("vacatedYesCount",vacatedYesCount);
        map.put("vacatedNoCount",vacatedNoCount);
        map.put("approvingCount",approvingCount);
        map.put("approvedYesCount",approvedYesCount);
        map.put("approvedNoCount",approvedNoCount);
        return Result.success(map);
    }

    //统计approver待审批，审批通过，审批不通过的数据
    @PostMapping("/countApproved")
    public Result countApproved(@RequestBody JSONObject jsonObject){
        String approver = jsonObject.getStr("username");
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int   approvedYesCount , approvedNoCount , approvingCount ;
        //正在进行
        approvingCount = vacateService.count(new QueryWrapper<Vacate>().
                eq("approver",approver).eq("status","待审批"));
        //完成
        approvedYesCount = vacateService.count(new QueryWrapper<Vacate>().
                eq("approver",approver).eq("status","请假成功").
                or().eq("approver",approver).eq("status","待确认"));
        //完成
        approvedNoCount = vacateService.count(new QueryWrapper<Vacate>().
                eq("approver",approver).eq("status","请假失败").
                or().eq("approver",approver).eq("status","不通过"));

        map.put("approvedYesCount",approvedYesCount);
        map.put("approvedNoCount",approvedNoCount);
        map.put("approvingCount",approvingCount);
        return Result.success(map);
    }

    //统计请假类型数据
    @PostMapping("/countVacateByReason")
    public Result countVacateByReason(@RequestBody JSONObject jsonObject){
        Boolean isUser = jsonObject.getBool("isUser");
        String username = jsonObject.getStr("username");
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        int sickCount, thingCount , travelCount , otherCount;
        if (!isUser){
            //all
            //病假总数
            sickCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("approver",username).eq("reason","病假"));
            //事假总数
            thingCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("approver",username).eq("reason","事假"));
            //出差总数
            travelCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("approver",username).eq("reason","出差"));
            //其他总数
            otherCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("approver",username).eq("reason","其他"));

        }else{
            //user
            //病假总数
            sickCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("username",username).eq("reason","病假"));
            //事假总数
            thingCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("username",username).eq("reason","事假"));
            //出差总数
            travelCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("username",username).eq("reason","出差"));
            //其他总数
            otherCount = vacateService.count(new QueryWrapper<Vacate>().
                    eq("username",username).eq("reason","其他"));
        }
        map.put("sickCount",sickCount);
        map.put("thingCount",thingCount);
        map.put("travelCount",travelCount);
        map.put("otherCount",otherCount);
        return Result.success(map);
    }

    //统计最近七天请假数据
    @PostMapping("/countVacateByDay")
    public Result countVacateByDay(@RequestBody JSONObject jsonObject){
        Boolean isUser = jsonObject.getBool("isUser");
        String username = jsonObject.getStr("username");

        LocalDateTime now = LocalDateTime.now();
        String today = StrUtil.subBefore(now.toString(),"T",true);
        String before1day = StrUtil.subBefore(now.minusDays(1).toString(),"T",true);
        String before2day = StrUtil.subBefore(now.minusDays(2).toString(),"T",true);
        String before3day = StrUtil.subBefore(now.minusDays(3).toString(),"T",true);
        String before4day = StrUtil.subBefore(now.minusDays(4).toString(),"T",true);
        String before5day = StrUtil.subBefore(now.minusDays(5).toString(),"T",true);
        String before6day = StrUtil.subBefore(now.minusDays(6).toString(),"T",true);
        HashMap<String, Object> map = new HashMap<String, Object>();

        Integer todayCount, before1dayCount ,before2dayCount, before3dayCount , before4dayCount , before5dayCount ,before6dayCount;
        if (!isUser){
            //今天请假总数
            todayCount = vacateService.count(new QueryWrapper<Vacate>().eq("approver",username).like("created",today));
            //昨天请假总数
            before1dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("approver",username).like("created",before1day));
            //前2天请假总数
            before2dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("approver",username).like("created",before2day));
            //前3天请假总数
            before3dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("approver",username).like("created",before3day));
            //前4天请假总数
            before4dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("approver",username).like("created",before4day));
            //前5天请假总数
            before5dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("approver",username).like("created",before5day));
            //前6天请假总数
            before6dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("approver",username).like("created",before6day));

        }else{
            //user
            //今天请假总数
            todayCount = vacateService.count(new QueryWrapper<Vacate>().eq("username",username).like("created",today));
            //昨天请假总数
            before1dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("username",username).like("created",before1day));
            //前2天请假总数
            before2dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("username",username).like("created",before2day));
            //前3天请假总数
            before3dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("username",username).like("created",before3day));
            //前4天请假总数
            before4dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("username",username).like("created",before4day));
            //前5天请假总数
            before5dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("username",username).like("created",before5day));
            //前6天请假总数
            before6dayCount = vacateService.count(new QueryWrapper<Vacate>().eq("username",username).like("created",before6day));

        }
        map.put("today",today);map.put("todayCount",todayCount);
        map.put("before1day",before1day);map.put("before1dayCount",before1dayCount);
        map.put("before2day",before2day);map.put("before2dayCount",before2dayCount);
        map.put("before3day",before3day);map.put("before3dayCount",before3dayCount);
        map.put("before4day",before4day);map.put("before4dayCount",before4dayCount);
        map.put("before5day",before5day);map.put("before5dayCount",before5dayCount);
        map.put("before6day",before6day);map.put("before6dayCount",before6dayCount);
        return Result.success(map);
    }


}

